# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# Name:         driven
# Description:  
# Author:       Administrator
# Date:         2020/2/12
#-------------------------------------------------------------------------------

from guitest.unitdemo.testcacl import CaclTest
import unittest
class Driven:

	# 直接运行测试类中的测试方法
	@classmethod
	def start1(cls):
		unittest.main(verbosity=2)

	# 组织测试执行顺序
	@classmethod
	def start2(cls):
		# 创建测试套件
		ts = unittest.TestSuite()
		# 组织测试方法的执行顺序
		# ts.addTest(CaclTest('test_add'))
		# ts.addTest(CaclTest('test_sub'))
		# ts.addTest(CaclTest('test_mul'))
		# ts.addTest(CaclTest('test_div'))
		ts.addTests([CaclTest('test_add'),CaclTest('test_sub'),
		             CaclTest('test_div')])

		# 运行测试套.创建运行器
		runner = unittest.TextTestRunner(verbosity=2)
		# 使用运行器运行测试套
		runner.run(ts)

	# 使用加载器来组织测试用例
	@classmethod
	def start3(cls):
		ts = unittest.TestSuite()
		# 创建加载器对象
		loader = unittest.TestLoader()
		# 通过类名加载该类下的所有的测试方法。Name指该测试类的类全名
		tests1 = loader.loadTestsFromName('guitest.unitdemo.testcacl.CaclTest')
		tests2 = loader.loadTestsFromName('guitest.unitdemo.testlogin.LoginTest')
		# loader.loadTestsFromNames(['guitest.unitdemo.testcacl.CaclTest','guitest.unitdemo.testlogin.LoginTest'])
		# 将测试类中的所有测试方法添加至ts中
		ts.addTests([tests1,tests2])
		# ts.addTests(tests1)
		# ts.addTests(tests2)
		runner = unittest.TextTestRunner(verbosity=2)
		runner.run(ts)

	# 生成测试报告到文件中
	@classmethod
	def start4(cls):
		ts = unittest.TestSuite()
		loader = unittest.TestLoader()
		tests1 = loader.loadTestsFromName('guitest.unitdemo.testcacl.CaclTest')
		tests2 = loader.loadTestsFromName('guitest.unitdemo.testlogin.LoginTest')
		ts.addTests([tests1, tests2])
		with open('result.txt','w') as file:
			# 将运行后的结果放在哪里。通过流的方式写入文件
			runner = unittest.TextTestRunner(stream=file,verbosity=2)
			runner.run(ts)

	# 使用第三方库生成HTML格式的测试报告
	@classmethod
	def start5(cls):
		from HTMLTestRunner import HTMLTestRunner
		ts = unittest.TestSuite()
		loader = unittest.TestLoader()
		tests1 = loader.loadTestsFromName('guitest.unitdemo.testcacl.CaclTest')
		tests2 = loader.loadTestsFromName('guitest.unitdemo.testlogin.LoginTest')
		ts.addTests([tests1, tests2])
		with open('report.html','w') as file:
			html_runner = HTMLTestRunner(stream=file,verbosity=2)
			html_runner.run(ts)


if __name__ == '__main__':
	Driven.start5()